package world.criver.h50909.tool;

import org.springframework.stereotype.Component;
import world.criver.h50909.data.daSqlite;
import world.criver.h50909.entity.entEquipmentInfo;
import world.criver.h50909.entity.entEquipmentSummary;
import world.criver.h50909.entity.entName;

import java.util.List;
import java.util.Random;
@Component
public class toolEquipmentInfo {
    /**
     * 随机抽取一个设备名
     * @return
     */
    public String getRandomEquipment(){
        List<entName> names= daSqlite.tool.query("select name from equipment_info", entName.class);
        if(names.isEmpty())return "";
        return names.get(new Random().nextInt(names.size())).getName();
    }

    /**
     * 获取设备的图片
     * @param name
     * @return
     */
    public String getEquipmentImage(String name){
        daSqlite.lock.lock();
        List<entName> ims=daSqlite.tool.query("select image as name from equipment_info where name='"+name+"'", entName.class);
        daSqlite.lock.unlock();
        if(ims.isEmpty())return "equipment_normal";
        return ims.get(0).getName();
    }

    /**
     * 获取所有设备概要
     * @return
     */
    public List<entEquipmentSummary> querySummaryList(){
        daSqlite.lock.lock();
        List<entEquipmentSummary> as= daSqlite.tool.query("select image,name,protocol,url from equipment_info",entEquipmentSummary.class);
        daSqlite.lock.unlock();
        return as;
    }

    /**
     * 获取所有设备信息
     * @return
     */
    public List<entEquipmentInfo> queryList(){
        try {
            daSqlite.lock.lock();
            List<entEquipmentInfo> as= daSqlite.tool.query("select * from equipment_info", entEquipmentInfo.class);
            daSqlite.lock.unlock();
            return as;
        }catch (Exception e){
            daSqlite.lock.unlock();
            return null;
        }
    }

    /**
     * 添加设备
     * @param info
     * @return
     */
    public boolean add(entEquipmentInfo info){
        daSqlite.lock.lock();
        boolean a= daSqlite.tool.update("insert into equipment_info values('"+info.getName()+"','"+info.getImage()+"','"+info.getModel()+"','"+
                info.getMethod()+"','"+info.getProtocol()+"','"+info.getUrl()+"','"+info.getAlarmKey()+"','"+info.getProcessKey()+"','"+
                info.getAwaitKey()+"','"+info.getProgramKey()+"','"+info.getSpeedKey()+"',"+info.getTimestamp()+")")>0;
        daSqlite.lock.unlock();
        return a;
    }

    /**
     * 更新设备
     * @param info
     * @param name
     * @return
     */
    public boolean update(entEquipmentInfo info,String name){
        daSqlite.lock.lock();
        boolean a= daSqlite.tool.update("update equipment_info set name='"+info.getName()+"',image='"+info.getImage()+"',model='"+
                info.getModel()+"',method='"+info.getMethod()+"',protocol='"+info.getProtocol()+"',url='"+info.getUrl()+"',alarmKey='"+
                info.getAlarmKey()+"',processKey='"+info.getProcessKey()+"',awaitKey='"+info.getAwaitKey()+"',programKey='"+
                info.getProgramKey()+"',speedKey='"+info.getSpeedKey()+"',timestamp="+info.getTimestamp()+" where name='"+name+"'")>0;
        daSqlite.lock.unlock();
        return a;
    }

    /**
     * 设备详情
     * @param name
     * @return
     */
    public entEquipmentInfo info(String name){
        daSqlite.lock.lock();
        List<entEquipmentInfo> as=daSqlite.tool.query("select * from equipment_info where name='"+name+"'",entEquipmentInfo.class);
        daSqlite.lock.unlock();
        if(as.isEmpty())return new entEquipmentInfo();
        return as.get(0);
    }

    /**
     * 删除设备
     * @param name
     * @return
     */
    public boolean delete(String name){
        daSqlite.lock.lock();
        daSqlite.tool.update("delete from equipment_info where name='"+name+"'");
        daSqlite.tool.update("delete from equipment_state where name='"+name+"'");
        daSqlite.lock.unlock();
        return true;
    }
}